package org.rednebula.framework.core.model.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.rednebula.framework.core.IEnvironment;
import org.rednebula.framework.core.log.Log;
import org.rednebula.framework.core.model.ListenerSwitch;
import org.rednebula.framework.core.model.State;


public abstract  class BasicModel implements IModel,
		Serializable {

	State state;// 当前model状态
	
	IModel parent;

	Object ident;

	ListenerSwitch lSwitch = ListenerSwitch.OFF; // 是否监听属性的变化

	Map<Class, State> childrenState = new HashMap<Class, State>(8); // 保持子model
																	// 集合
																	// 是否修改的状态


	StateChangeRule changeRule=StateChangeRule.getInstance();
	
	protected static Log logger=Log.getLog(BasicModel.class);


 
	public BasicModel() {
		super();
		this.state = State.NORMAL;
	}
	

	 /**
     * 如果是新增的或临时的，可以这个构造函数
     * 
     */
	public BasicModel(State state) {
		super();
		this.state = state;
	}

	public void setState(State modelState) {
		this.state = modelState;
	}

	public Object getIdent() {
		return ident;
	}


	public void setIdent(Object ident) {
		this.ident = ident;
	}

    
	public State getState() {
		return state;
	}

	protected ListenerSwitch getlSwitch() {
		if (getParent() != null)
			return ((BasicModel) getParent()).getlSwitch();
		return lSwitch;
	}

	public void changeStateDelete() {
		changeState(State.DELETE);
	}

	public void changeStateNormal() {
		changeState( State.NORMAL);
	}

	public void changeStateCreate() {
		changeState(State.CREATE);
	}

	public void changeStateModify() {
		changeState(State.MODIFY);
	}
    
	
	public void setlSwitch(ListenerSwitch lSwitch) {
		this.lSwitch = lSwitch;
	}

	public IModel getParent() {
		return parent;
	}

	public void setParent(IModel parent) {
		this.parent = parent;
	}

	public State getChildrensState(Class cls) {
		State s = childrenState.get(cls);
		if (s == null)
			s = State.NORMAL;
		return s;
	}

	private void pupChildrenState(Class key, State value) {
		childrenState.put(key, value);
	}
    
	/**
	 * 打开监听
	 */
	public void openListener(){
		this.setlSwitch(ListenerSwitch.ON);
	}
	/**
	 * 关闭监听
	 */
	public void closeListener(){
		this.setlSwitch(ListenerSwitch.OFF);
	}
	/**
	 * 监听状态是否打开
	 * 
	 * @return
	 */
	public boolean isOpen() {
		return (ListenerSwitch.ON == getlSwitch());
	}
    
	/**
	 * 当前状态是否是新增的
	 * @return
	 */
	public boolean isCreated(){
		return (State.CREATE==this.state);
	}
	/**
	 * 当前状态是否是修改过
	 * @return
	 */
	public boolean isModified(){
		return (State.MODIFY==this.state);
	}
	
	/**
	 * 当前状态是否是删除
	 * @return
	 */
	public boolean isDeleted(){
		return (State.DELETE==this.state);
	}
	
	/**
	 * 当前状态是否没被修改过
	 * @return
	 */
	public boolean isNormaled(){
		return (State.NORMAL==this.state);
	}
	
	/**
	 * 当前状态是否暂时的
	 * @return
	 */
	public boolean isTransiented(){
		return (State.TRANSIENT==this.state);
	}
	/**
	 * 当调用该model的 set开始的方法是调用此方法
	 * 
	 * 该变当前model的状态为修改状态
	 */
	public void changeState(State state) {
		if (isOpen()) {
			state=getChangeRule().change(state, this.getState());
			this.setState(state);
		}
	}


	public void changeChildrenState(Class cls, State state) {
		if (isOpen()) {
			pupChildrenState(cls, state);
		}
	}
	
	public void clearChildrenState(){
		childrenState.clear();
	}
	protected StateChangeRule getChangeRule(){
		return changeRule;
	}
	
	public void create(IEnvironment env) {
		// TODO Auto-generated method stub

	}

	public void delete(IEnvironment env) {
		// TODO Auto-generated method stub

	}

	public void load(IEnvironment env) {
		// TODO Auto-generated method stub

	}

	public void modify(IEnvironment env) {
		// TODO Auto-generated method stub

	}

	public void other(IEnvironment env) {
		// TODO Auto-generated method stub
		
	}
     
	
}
